/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.muddzdev.styleabletoast.demo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;

import com.muddzdev.styleabletoast.styleabletoast.StyleableToast;

/**
 * xinglu
 *
 * @since 2021-04-20
 */
public class MainAbility extends Ability {
    String toastMsg = "Hello World!";
    Color color = new Color(Consts.COLOR);
    Color defaultTextColor = new Color(Consts.DEFAULT_COLOR);
    Color defaultBackgroundColor = new Color(Consts.DEFAULT_BACKGROUND_COLOR);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(Color.getIntColor("#00554B"));
        super.setUIContent(ResourceTable.Layout_ability_main);
        initview();
    }

    private void initview() {
        Button bt1 = (Button) findComponentById(ResourceTable.Id_bt1);
        Button bt2 = (Button) findComponentById(ResourceTable.Id_bt2);
        Button bt3 = (Button) findComponentById(ResourceTable.Id_bt3);
        Button bt4 = (Button) findComponentById(ResourceTable.Id_bt4);
        Button bt5 = (Button) findComponentById(ResourceTable.Id_bt5);
        Button bt6 = (Button) findComponentById(ResourceTable.Id_bt6);
        Button bt7 = (Button) findComponentById(ResourceTable.Id_bt7);
        Button bt8 = (Button) findComponentById(ResourceTable.Id_bt8);
        Button bt9 = (Button) findComponentById(ResourceTable.Id_bt9);
        Button bt10 = (Button) findComponentById(ResourceTable.Id_bt10);

        bt1.setClickedListener(this::onClick);
        bt2.setClickedListener(this::onClick);
        bt3.setClickedListener(this::onClick);
        bt4.setClickedListener(this::onClick);
        bt5.setClickedListener(this::onClick);
        bt6.setClickedListener(this::onClick);
        bt7.setClickedListener(this::onClick);
        bt8.setClickedListener(this::onClick);
        bt9.setClickedListener(this::onClick);
        bt10.setClickedListener(this::onClick);

        bt1.setLongClickedListener(this::onLongClick);
        bt2.setLongClickedListener(this::onLongClick);
        bt3.setLongClickedListener(this::onLongClick);
        bt4.setLongClickedListener(this::onLongClick);
        bt5.setLongClickedListener(this::onLongClick);
        bt6.setLongClickedListener(this::onLongClick);
        bt7.setLongClickedListener(this::onLongClick);
        bt8.setLongClickedListener(this::onLongClick);
        bt9.setLongClickedListener(this::onLongClick);
        bt10.setLongClickedListener(this::onLongClick);

    }


    private void onLongClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bt1:
                new StyleableToast.Builder(getContext())
                    .text(toastMsg)
                    .backgroundColor(color)
                    .textColor(defaultTextColor)
                    .show();
                break;
            case ResourceTable.Id_bt2:
                new StyleableToast.Builder(getContext())
                    .text(toastMsg)
                    .textColor(color)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt3:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .textColor(color)
                    .textBold()
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt4:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .font(Consts.TEXT_STYLE_NAME)
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt5:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .cornerRadius(Consts.CORNER_RADIUS)
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt6:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .iconStart(getIcon())
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt7:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .iconEnd(getIcon())
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt8:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .iconStart(getIcon())
                    .iconEnd(getIcon())
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt9:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .stroke(Consts.STROKE, color)
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt10:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .stroke(Consts.STROKE, Color.BLACK)
                    .backgroundColor(Color.WHITE)
                    .solidBackground()
                    .textColor(Color.RED)
                    .textBold()
                    .font(Consts.TEXT_STYLE_NAME)
                    .iconStart(getIcon())
                    .iconEnd(getIcon())
                    .cornerRadius(Consts.ANOTHER_CORNER_RADIUS)
                    .textSize(Consts.TEXT_SIZE)
                    .show();
                break;
            default:
                break;
        }
    }


    private void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bt1:
                new StyleableToast.Builder(getContext())
                    .text(toastMsg)
                    .backgroundColor(color)
                    .textColor(defaultTextColor)
                    .show();

                break;
            case ResourceTable.Id_bt2:
                new StyleableToast.Builder(getContext())
                    .text(toastMsg)
                    .textColor(color)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt3:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .textColor(color)
                    .textBold()
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt4:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .font("dosis.otf")
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt5:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .cornerRadius(Consts.CORNER_RADIUS)
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt6:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .iconStart(getIcon())
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt7:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .iconEnd(getIcon())
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt8:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .iconStart(getIcon())
                    .iconEnd(getIcon())
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt9:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .stroke(Consts.STROKE, color)
                    .textColor(defaultTextColor)
                    .backgroundColor(defaultBackgroundColor)
                    .show();
                break;
            case ResourceTable.Id_bt10:
                new StyleableToast.Builder(this)
                    .text(toastMsg)
                    .stroke(Consts.STROKE, Color.BLACK)
                    .backgroundColor(defaultBackgroundColor)
                    .solidBackground()
                    .textColor(Color.RED)
                    .textBold()
                    .font("dosis.otf")
                    .iconStart(getIcon())
                    .iconEnd(getIcon())
                    .cornerRadius(Consts.ANOTHER_CORNER_RADIUS)
                    .textSize(Consts.TEXT_SIZE)
                    .show();
                break;
            default:
                break;
        }
    }

    public int getIcon() {
        return ResourceTable.Media_ic_autorenew_black_24dp;
    }
}
